package com.kingbook.services;

import com.kingbook.core.base.SupportService;
import com.kingbook.entity.Customer;
import com.kingbook.entity.Driver;
import com.kingbook.entity.Workspace;
import com.kingbook.core.base.BaseService;
import com.kingbook.core.result.JSONPageResult;
import com.kingbook.core.result.JSONResult;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @Author Bucky
 * @Date 2019-12-18 21:37:35
 * @Version 1.0
 */
@Slf4j
@Service
public class WorkspaceService extends SupportService {

    @Autowired
    CustomerService customerService;
    @Autowired
    PartnerService partnerService;

    @PostConstruct
    private void init() {
        setNamespace("Workspace");
    }

    public JSONResult updateWorkspace( Map<String, Object> param ) {
        int mid = MapUtils.getIntValue(param, "mid");
        int id = MapUtils.getIntValue(param, "id");
        String address = MapUtils.getString(param, "address");
        String from = MapUtils.getString(param, "from");
        String owner = MapUtils.getString(param, "owner");
        if (from.equals("customer"))
            param.put("cid", customerService.getCustomerId(mid, owner, false));
        else
            param.put("pid", partnerService.getPartnerId(mid, owner, false));
        Workspace workspace =  queryWorkspaceByName( mid, address);
        if (workspace != null && workspace.getId() !=id && address.equals(workspace.getName()))
            return JSONResult.failed(address + " 已注册");
        return id > 0 ? modify(param) : save(param);
    }

    public int getWorkspaceIdByCid(int mid, long cid, String address) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("mid", mid);
        param.put("address", address);
        param.put("cid", cid);
        Workspace o = findObject("queryWorkspace", param, Workspace.class);
        if (o != null) {
            return o.getId();
        } else {
            JSONResult result = insertReturnKey("insert", param);
            return Integer.parseInt(result.getMsg());
        }
    }

    public int getWorkspaceIdByPid(int mid, long pid, String address) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("mid", mid);
        param.put("address", address);
        param.put("pid", pid);
        Workspace o = findObject("queryWorkspace", param, Workspace.class);
        if (o != null) {
            return o.getId();
        } else {
            param.put("name", address);
            JSONResult result = insertReturnKey("insert", param);
            return Integer.parseInt(result.getMsg());
        }
    }

    private Workspace queryWorkspaceByName(Integer mid, String address) {
        Map<String, Object> param = new HashMap<>(2);
        param.put("mid", mid);
        param.put("address", address);
        return findObject("queryWorkspace", param, Workspace.class);
    }

    public int getRows(int id, int mid) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("id", id);
        param.put("mid", mid);
        return getRows("getRows", param);
    }

}
